package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.base.service.*;
import cn.wolfcode.p2p.base.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LofinInfoServiceImpl implements ILofinInfoService {
    @Autowired
    private LoginInfoMapper infoMapper;
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IIpLogService ipLogService;

    @Override
    public void register(String username, String verifycode, String password, String confirmPwd) {
        //判断是否为null的校验
        AssertUtil.isNotNull(username, "用户名不能为空");
        AssertUtil.isNotNull(verifycode, "验证码不能为空");
        AssertUtil.isNotNull(password, "密码不能为空");
        AssertUtil.isNotNull(confirmPwd, "验证密码不能为空");
        AssertUtil.idEqual(password, confirmPwd, "两次密码输入不一致");
        //校验验证码是否有效 TODO
        boolean isVaild = verifyCodeService.validate(username, verifycode);
        if (!isVaild) {
            throw new DisplayableExcetion("验证码有误,请重新输入");
        }
        //判断用户名,手机号是否被注册
        int count = infoMapper.selectCountByUsername(username);
        if (count > 0) {
            throw new DisplayableExcetion("该手机号码被注册了");
        }
        //将注册的用户存进设局库,密码要进行加密加盐设置
        LoginInfo info = new LoginInfo();
        info.setUsername(username);
        info.setPassword(MD5.encoder(password));
        info.setUserType(LoginInfo.USERTYPE_USER);//设置为普通用户
        infoMapper.insert(info);
        Account account = new Account();
        account.setId(info.getId());
        accountService.insert(account);
        UserInfo userInfo = new UserInfo();
        userInfo.setPhoneNumber(username);
        userInfo.setId(info.getId());
        userInfoService.insert(userInfo);
    }

    //姜茶用户名是否存在
    public int selectCountByUsername(String username) {
        int count = infoMapper.selectCountByUsername(username);
        if (count > 0) {
            throw new DisplayableExcetion("该用户已存在");
        }
        return 1;
    }

    //账户登录
    @Override
    public LoginInfo userLogin(String username, String password,int userType) {
        LoginInfo login = infoMapper.login(username, MD5.encoder(password),userType);
        //保存登录日志
        IpLog ipLog = new IpLog();
        ipLog.setUsername(username);
        ipLog.setIp(UserContext.getIP());
        ipLog.setLoginTime(new Date());
        ipLog.setUserType(userType);
        if (login != null) {
            //将用户存进session
            UserContext.setCount(login);
            ipLog.setState(IpLog.STATE_SUCCESS);
        } else {
            ipLog.setState(IpLog.STATE_FAILED);
        }
        ipLogService.insert(ipLog);
        return login;
    }

    //初始化管理员
    @Override
    public void initAdmin() {
        //判断数据库中又饿米有管理员
        int count = infoMapper.selecCountByUserType(LoginInfo.USERTYPE_MANAGER);
        //如果没有就创建一个
        if (count == 0) {
            LoginInfo info = new LoginInfo();
            info.setUsername(Constants.DEFAULT_ADMIN_ACCOUNT);
            info.setPassword(MD5.encoder(Constants.DEFAULT_ADMIN_PASSWORD));
            info.setUserType(LoginInfo.USERTYPE_MANAGER);
            infoMapper.insert(info);
        }
    }

    //查询所有的客服
    @Override
    public List<LoginInfo> selectIsSuditorList() {
        return infoMapper.selectIsSuditorList();
    }

    @Override
    public LoginInfo get(Long id) {
        return infoMapper.selectByPrimaryKey(id);
    }
}
